package org.etcsoft.examples.saniker;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;

public class FileTableLabelProvider implements ITableLabelProvider {
	private Map imageCache = new HashMap(11);
	
	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		
		if(columnIndex == 0) {
			ImageDescriptor descriptor = null;
			
		    if(((File) element).isDirectory())
		    {
		    	if(((File) element).getParent() == null)
		    	{
		    		descriptor = ImageDescriptor.createFromFile(this.getClass(), "icon/icon_ClosedDrive.gif");
		    	} else 
		    	{
		    		descriptor = ImageDescriptor.createFromFile(this.getClass(), "icon/icon_ClosedFolder.gif");	
		    	}
		    	
		    } else {
		    	descriptor = ImageDescriptor.createFromFile(this.getClass(), "icon/icon_File.gif");
		    }
		    
		    Image image = (Image) imageCache.get(descriptor);
		    
			if (image == null) {
				image = descriptor.createImage();
				imageCache.put(descriptor, image);
			}
			
			return image;
		}
		return null;
	}
	
	@Override
	public String getColumnText(Object element, int columnIndex) {
		String rtStr = null;
		// TODO Auto-generated method stub
		if(columnIndex == 0) {
			rtStr = ((File) element).getName();
		}		
	    else if(columnIndex == 1) {
	    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd aa hh:mm:ss");
		    rtStr = formatter.format(((File) element).lastModified());
		}  
		else if(columnIndex == 2) {
			rtStr = String.format("%,d", ((File) element).length()) + " byte";
		}
		
		return rtStr; 
	}
	
	@Override
	public void addListener(ILabelProviderListener listener) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener listener) {
		// TODO Auto-generated method stub
		
	}

}
